import { _decorator, AudioClip, Color, Component, Node, Sprite, SpriteFrame, tween, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import CardFunctions from "../Common/CardFunctions";
import { Consts } from "../Data/Consts";
import { GameData } from "../Data/GameData";
import { BoardLayout } from "../Data/GameEnum";
import { MaterialData } from "../Config/MaterialData";
import { bdlMgr } from "../Manager/SS_BundleManager";
import SS_TimerManager from "../Manager/SS_TimerManager";
import Card from "./Card";
import { SS_AudioManager } from '../Manager/SS_AudioManager';
import { SS_Settings } from '../Data/SS_Settings';

/**Board */
@ccclass('Board')
export default class Board extends Component {
    @property({ type: Node })
    public Board_h: Node | null = null;
    @property({ type: Node })
    public Board_v: Node | null = null;
    @property({ type: Node })
    public cover_h: Node | null = null;
    @property({ type: Node })
    public cover_v: Node | null = null;
    @property({ type: Node })
    public collider_h: Node | null = null;
    @property({ type: Node })
    public collider_v: Node | null = null;
    main_board: Node;
    cover: Node | null = null;
    collider: Node | null = null;
    x: number = 0;
    y: number = 0;
    layer: number = 0;
    layout;
    data_cardList;
    cardList;
    isCover: boolean = false;
    length: number = 3;
    w: number = 0;
    h: number = 0;
    pos;
    onLoad() {

    }
    async Init(_x: number, _y: number, _layer: number, _layout: any, _data_cardList: any) {
        this.x = _x;
        this.y = _y;
        this.layer = _layer;
        // console.log("this.layer", this.layer);
        this.layout = _layout;
        this.data_cardList = _data_cardList;

        //        // console.log("this.data_cardList", this.data_cardList);

        let offset_y = CardFunctions.GetLayerYOffset(this.layer);
        // this.node.position.x = this.x;
        // this.node.y = this.y + offset_y;

        this.node.setPosition(this.x, this.y + offset_y, 0);
        this.pos = v3(this.x, this.y + offset_y, 0);

        if (this.layout === BoardLayout.h) {
            this.main_board = this.Board_h;
            this.cover = this.cover_h;
            this.collider = this.collider_h;
            this.w = 216;
            this.h = 72;
        }
        else if (this.layout === BoardLayout.v) {
            this.main_board = this.Board_v;
            this.cover = this.cover_v;
            this.collider = this.collider_v;
            this.w = 72;
            this.h = 216;
        }

        this.cardList = this.main_board.getComponentsInChildren(Card);

        for (let index = 0; index < _data_cardList.length; index++) {
            const id = _data_cardList[index];
            const item_card: Card = this.cardList[index];
            item_card.id = id;
            item_card.board = this;

            if (id > 0) {
                const item_data = MaterialData.find(e => e.id === id);
                bdlMgr.getRes<SpriteFrame>(Consts.BundleName.SPRITE, `main/material/icon/${item_data.icon}/spriteFrame`, SpriteFrame).then(res => { item_card.Sprite_icon.spriteFrame = res });


                const data = MaterialData.find(e => e.id === item_card.id);

                item_card.Sprite_frame.getComponent(Sprite).color = new Color().fromHEX(`#${data.color_frame}`);
                item_card.Sprite_frame.node.active = false;

                item_card.Sprite_icon.node.setScale(1, 1, 1);
                // item_card.Sprite_icon.node.y = 4;
                item_card.Sprite_icon.node.setPosition(item_card.Sprite_icon.node.position.x, 4, item_card.Sprite_icon.node.position.z);

                // item_card.Sprite_bg.node.width = 72;
                // item_card.Sprite_bg.node.height = 76;
            }
            else {
                // item_card.node.destroy();
                item_card.node.active = false;
            }
        }

        this.AddListener();
        this.Refresh();
    }

    GetLength() {
        let length = 0;

        for (let index = 0; index < this.cardList.length; index++) {
            const card: Card = this.cardList[index];
            if (card.id > 0 && card.is装填 === false) {
                length++;
            }
        }

        return length;
    }

    GetCard1() {
        for (let index = 0; index < this.cardList.length; index++) {
            const card: Card = this.cardList[index];
            if (card.id > 0 && card.is装填 === false) {
                return card;
            }
        }
    }

    AddListener() {
        //        // this.main_board.on(cc.Node.EventType.TOUCH_START, this.TOUCH_START, this);
    }

    //    // async TOUCH_START(t) {
    //    //     const array = CardFunctions.GetHighLayerBoardArray(this);
    //    //     for (let index = 0; index < array.length; index++) {
    //    //         const board = array[index];
    //    //         cc.tween(board.node)
    //    //             .to(0.1, { scale: 1.2 }, { easing: "" })
    //    //             .to(0.1, { scale: 1 }, { easing: "" })
    //    //             .start();
    //    //         // }
    //    //     }
    //    // }

    Refresh() {
        this.RefreshIcon();
        this.Refresh_layout();
        //        // this.RefreshCover();
    }

    async Refresh_layout() {
        this.Board_h.active = false;
        this.Board_v.active = false;

        if (this.layout === BoardLayout.h) {
            this.Board_h.active = true;
        }
        else if (this.layout === BoardLayout.v) {
            this.Board_v.active = true;
        }
    }

    async RefreshIcon() {

    }

    async RefreshCover() {
        const isBoardCover = CardFunctions.IsBoardCover(this);
        this.cover.active = isBoardCover;
    }

    SetCover() {
        this.isCover = CardFunctions.IsBoardCover(this);
        this.RefreshCover();
    }

    Check撤凳子() {
        let count = 3// this.cardList.length;
        // console.log(`count----1`, count);

        for (let index = 0; index < this.cardList.length; index++) {
            const item_card: Card = this.cardList[index];
            // console.log(`item_card.id`, item_card.id);
            // console.log(`item_card.is装填`, item_card.is装填);

            if (item_card.id <= 0 || item_card.is装填 === true) {
                count--;
            }
        }

        // console.log(`count----2`, count);

        if (count <= 0) {
            return true;
        }

        return false;
    }

    async Dead() {
        const i = GameData.arrBoard.indexOf(this);
        if (i > -1) {
            GameData.arrBoard.splice(i, 1);
        }

        this.cardList = [];

        tween(this.node)
            .by(0.1, { position: new Vec3(0, 30, 0) },)
            .to(0.1, { scale: new Vec3(0.4, 0.4, 0.4) })
            .call(async () => {
                let bomb = await CardFunctions.CreateBomb(this.node.worldPosition);
                // let p1 = this.node.convertToWorldSpaceAR(cc.v3(0, 0, 0));
                // let p2 = bomb.parent.convertToNodeSpaceAR(p1);
                // bomb.position = p2;

                bdlMgr.getRes<AudioClip>(Consts.BundleName.AUDIO, `sound/烟雾消散`, AudioClip).then(audio => {
                    SS_AudioManager.Instance.playOneShot(audio, SS_Settings.curMusicV);
                });

                this.node.destroy();

                await SS_TimerManager.AwaitTime(0.21);
                bomb.destroy();
            })
            .start();

        await SS_TimerManager.AwaitTime(0.1);
    }
}